/*********************************************************
 * copyright learnren.com 版权所有 
 * 开发人员：ivan.yu
 * 创建时间：2013/4/28 16:49:27
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.UI;
using LEARNREN.COM.TOOL;

namespace TopSite.Account
{
    public partial class CatalogueList : BasePage
    {
        CatalogueLogic CatalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitCatalogueTree();
            InitCatalogueSelector();
        }

        protected void CatalogueTree1_SelectedNodeChanged(object sender, LEARNREN.COM.WebSite.DelegateAndArges.CatalogueTreeSelectedNodeChangedEventArgs e)
        {
            long id = e.SelectedCatalogueId;

            btnSaveCatalogue.CommandArgument = EditStateEnum.Edit.ToString();
            Catalogue catalogue = CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
            this.txtTitle.Text = catalogue.Title;
            this.Order.Text = catalogue.Order.ToString();
            this.KeyWords.Text = catalogue.KeyWords;
            this.Summary.Text = catalogue.Summary;
            this.txtId.Value = catalogue.Id.ToString();
            this.ddlType.SelectedValue = catalogue.CatalogueType.ToString();
            this.SelectCatalogueDropDownList1.SelectedCatalogueId = catalogue.ParentId;
        }

        private void InitCatalogueSelector()
        {
            SelectCatalogueDropDownList1.CatalogueLogicProvider = CatalogueLogic;
            SelectCatalogueDropDownList1.CanClickNodeType = CatalogueTypeEnum.All;
        }

        private void InitCatalogueTree()
        {
            CatalogueTree1.CanClickNodeType = CatalogueTypeEnum.All;
            CatalogueTree1.CatalogueLogicProvider = CatalogueLogic;
            CatalogueTree1.IsShowId = true;
        }

        private void ReloadCatalogues()
        {
            CatalogueTree1.Reload();
            SelectCatalogueDropDownList1.Reload();
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            LinkButton linkbtn = sender as LinkButton;
            if (linkbtn != null)
            {
                string strId = linkbtn.CommandArgument;
                int id = 0;
                if (int.TryParse(strId, out id))
                {
                    btnSaveCatalogue.CommandArgument = EditStateEnum.Edit.ToString();
                    Catalogue catalogue = CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
                    this.txtTitle.Text = catalogue.Title;
                    this.Order.Text = catalogue.Order.ToString();
                    this.KeyWords.Text = catalogue.KeyWords;
                    this.Summary.Text = catalogue.Summary;
                    this.txtId.Value = catalogue.Id.ToString();
                    this.ddlType.SelectedValue = catalogue.CatalogueType.ToString();
                    this.SelectCatalogueDropDownList1.SelectedCatalogueId = catalogue.ParentId;
                }
            }
        }

        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            LinkButton linkbtn = sender as LinkButton;
            if (linkbtn != null)
            {
                string strId = linkbtn.CommandArgument;
                int id = 0;
                if (int.TryParse(strId, out id))
                {
                    Catalogue catalogue = CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
                    if (catalogue != null)
                    {
                        CatalogueLogic.Delete(catalogue);
                        BasicCache.AllCatalogueList.RemoveAll(p => p.Id == catalogue.Id);

                        ReloadCatalogues();
                    }
                }
            }
        }

        private Catalogue GetCatalogueForSave()
        {
            Catalogue result = null;

            string str = btnSaveCatalogue.CommandArgument;
            if (str == EditStateEnum.Edit.ToString())
            {
                int id = 0;
                if (int.TryParse(this.txtId.Value, out id))
                {
                    result = CatalogueLogic.GetList(p => p.Id == id).FirstOrDefault();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                result = new Catalogue();
                result.Id = CatalogueLogic.GetNewIdentity();
                result.CreatedTime = DateTime.Now;
            }


            result.Title = txtTitle.Text;
            result.KeyWords = KeyWords.Text;
            result.Summary = Summary.Text;
            result.CatalogueType = (CatalogueTypeEnum)Enum.Parse(typeof(CatalogueTypeEnum), ddlType.SelectedValue);
            result.Order = int.Parse(Order.Text.Trim());
            result.ParentId = SelectCatalogueDropDownList1.SelectedCatalogueId;
            result.Path = CatalogueLogic.GetPath(result);

            return result;
        }

        private void ClearControls()
        {
            ClearControlText(this.txtId, this.txtTitle, this.Order, this.KeyWords, this.Summary);
            this.btnSaveCatalogue.CommandArgument = EditStateEnum.New.ToString();
        }

        protected void btnSaveCatalogue_Click(object sender, EventArgs e)
        {
            try
            {
                Catalogue catalogue = GetCatalogueForSave();
                CatalogueLogic.Save(catalogue);
                AddScript("alert('保存栏目成功！');");

                BasicCache.AllCatalogueList.RemoveAll(p => p.Id == catalogue.Id);
                BasicCache.AllCatalogueList.Add(catalogue);

                ReloadCatalogues();
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('保存栏目失败！');");
            }
            finally
            {
                ClearControls();
            }
        }

        protected override void InitLogic()
        {
            CatalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            CatalogueLogic.Dispose();
            CatalogueLogic = null;
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string strId = this.txtId.Value;
            long id = 0;
            if (long.TryParse(strId, out id))
            {
                Catalogue catalogue = CatalogueLogic.GetById(id);
                if (catalogue != null)
                {
                    CatalogueLogic.Delete(catalogue);
                    BasicCache.AllCatalogueList.RemoveAll(p => p.Id == catalogue.Id);

                    ReloadCatalogues();
                    ClearControls();
                }
            }
        }
    }
}